<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/6/006
 * Time: 10:06
 */

namespace app\api\controller\v1;

use app\api\controller\BaseController;
use app\api\model\ProductComment as ProductCommentModel;
use app\api\validate\CommentValidate;
use app\api\validate\IDMustBePostiveInt;
use app\lib\exception\CommentException;
use app\lib\exception\SuccessMessage;

class Comment extends BaseController {

    /**
     * 根据产品ID获取评论信息
     * @param $id
     * @return mixed
     * @throws CommentException
     */
    public function getProductCommentByProductId($id) {
        (new IDMustBePostiveInt())->goCheck();
        $comment = ProductCommentModel::getProductCommentByProductId($id);

        return $comment;
    }

    /**
     * 添加评论信息
     * @return \think\response\Json
     */
    public function addComment() {
        $commentValidate = new CommentValidate();
        if (input('?param.pm_comment_content') && input('param.pm_comment_content') == 1) {
            $commentValidate->goCheck();
        } else {
            $commentValidate->goCheck('no_pm');
        }
        $params = $commentValidate->getDataByRule(input('param.'));

        if (input('?post.image')) {
            $image = input('post.image/a');
            $params['image'] = $image;
        }

        if (input('?post.pm_image')) {
            $image = input('post.pm_image/a');
            $params['image'] = $image;
        }

        ProductCommentModel::addComment($params);

        return json(new SuccessMessage(), 201);
    }
}